﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerFixedAssetDao : IFixedAssetDao
    {
        /// <summary>
        /// Gets the accounts for is inventory item.
        /// </summary>
        /// <returns>List{FixedAsset}.</returns>
        public List<FixedAsset> GetFixedAssetsForIsInventoryItem()
        {
            throw new NotImplementedException();
        }

        public List<FixedAsset> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_FixedAsset";
            object[] parms = { "@WhereClause", null, "@OrderBy", "FixedAssetCode", "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_FixedAsset";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        string IFixedAssetDao.InsertFixedAsset(FixedAsset fixedAsset)
        {
            const string sql = "uspInsert_FixedAsset";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(fixedAsset));
        }


        public string UpdateFixedAsset(FixedAsset fixedAsset)
        {
            const string sql = "uspUpdate_FixedAsset";
            return Db.Update(sql, CommandType.StoredProcedure, Take(fixedAsset));
        }

        public string DeleteFixedAsset(FixedAsset fixedAsset)
        {
            const string sql = @"uspDelete_FixedAsset";
            object[] parms = { "@FixedAssetID", fixedAsset.FixedAssetId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }


        public FixedAsset GetFixedAsset(int fixedAssetId)
        {
            const string sql = @"uspGet_FixedAsset_ByID";
            object[] parms = { "@FixedAssetID", fixedAssetId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<FixedAsset> GetFixedAssetsByParent(int parentId)
        {
            const string sql = @"uspGet_FixedAsset_ByCondition";
            object[] parms = { "@WhereClause", " WHERE ParentID = " + parentId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<FixedAsset> GetFixedAssetsForComboTree(int fixedAssetId)
        {
            //const string sql = @"uspGet_FixedAsset_ForComboTreee";
            //object[] parms = { "@FixedAssetID", fixedAssetId };
            //return Db.ReadList(sql, true, Make, parms);

            const string sql = @"uspGet_FixedAsset_ByUnit";
            object[] parms = { "@UnitID", 1 };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, FixedAsset> Make = reader =>
            new FixedAsset
            {
                FixedAssetId = reader["FixedAssetID"].AsInt(),
                FixedAssetCode = reader["FixedAssetCode"].AsString(),
                FixedAssetName = reader["FixedAssetName"].AsString(),
                FixedAssetCategoryId = reader["FixedAssetCategoryID"].AsInt(),
                State = reader["State"].AsInt(),
                Description = reader["Description"].AsString(),
                ProductionYear = reader["ProductionYear"].AsInt(),
                MadeIn = reader["MadeIn"].AsString(),
                PurchasedDate = reader["PurchasedDate"].AsDateTime(),
                UsedDate = reader["UsedDate"].AsDateTime(),
                DepreciationDate = reader["DepreciationDate"].AsDateTime(),
                IncrementDate = reader["IncrementDate"].AsDateTime(),
                DisposedDate = reader["DisposedDate"].AsDateTime(),
                Unit = reader["Unit"].AsString(),
                SerialNumber = reader["SerialNumber"].AsString(),
                Accessories = reader["Accessories"].AsString(),
                Quantity = reader["Quantity"].AsInt(),
                UnitPrice = reader["UnitPrice"].AsDecimal(),
                OrgPrice = reader["OrgPrice"].AsDecimal(),
                AccumDepreciationAmount = reader["AccumDepreciationAmount"].AsDecimal(),
                RemainingAmount = reader["RemainingAmount"].AsDecimal(),
                AnnualDepreciationAmount = reader["AnnualDepreciationAmount"].AsDecimal(),
                LifeTime = reader["LifeTime"].AsDecimal(),
                DepreciationRate = reader["DepreciationRate"].AsDecimal(),
                OrgPriceAccountCode = reader["OrgPriceAccountCode"].AsString(),
                DepreciationAccountCode = reader["DepreciationAccountCode"].AsString(),
                CapitalAccountCode = reader["CapitalAccountCode"].AsString(),
                DepartmentId = reader["DepartmentID"].AsInt(),
                EmployeeId = reader["EmployeeID"].AsInt(),
                IsActive = reader["IsActive"].AsBool(),
                CapitalMoneyAccountCode = reader["CapitalMoneyAccountCode"].AsString(),
                ExpenseAccountCode = reader["ExpenseAccountCode"].AsString(),
                UnitId = reader["UnitID"].AsLong(),
            };

        /// <summary>
        /// Takes the specified account.
        /// </summary>
        /// <returns></returns>
        private object[] Take(FixedAsset fixedAsset)
        {
            return new object[]  
            {
                "@FixedAssetID", fixedAsset.FixedAssetId,
                "@FixedAssetCode", fixedAsset.FixedAssetCode,
                "@FixedAssetName", fixedAsset.FixedAssetName,
                "@FixedAssetCategoryID", fixedAsset.FixedAssetCategoryId,
                "@State", fixedAsset.State,
                "@Description", fixedAsset.Description,
                "@ProductionYear", fixedAsset.ProductionYear,
                "@MadeIn", fixedAsset.MadeIn,
                "@PurchasedDate", fixedAsset.PurchasedDate,
                "@UsedDate", fixedAsset.UsedDate,
                "@DepreciationDate", fixedAsset.DepreciationDate,
                "@IncrementDate", fixedAsset.IncrementDate,
                "@DisposedDate", fixedAsset.DisposedDate,
                "@Unit", fixedAsset.Unit,
                "@SerialNumber", fixedAsset.SerialNumber,
                "@Accessories", fixedAsset.Accessories,
                "@Quantity", fixedAsset.Quantity,
                "@UnitPrice", fixedAsset.UnitPrice,
                "@OrgPrice", fixedAsset.OrgPrice,
                "@AccumDepreciationAmount", fixedAsset.AccumDepreciationAmount,
                "@RemainingAmount", fixedAsset.RemainingAmount,
                "@AnnualDepreciationAmount", fixedAsset.AnnualDepreciationAmount,
                "@LifeTime", fixedAsset.LifeTime,
                "@DepreciationRate", fixedAsset.DepreciationRate,
                "@OrgPriceAccountCode", fixedAsset.OrgPriceAccountCode,
                "@DepreciationAccountCode", fixedAsset.DepreciationAccountCode,
                "@CapitalAccountCode", fixedAsset.CapitalAccountCode,
                "@DepartmentID", fixedAsset.DepartmentId,
                "@EmployeeID", fixedAsset.EmployeeId,
                "@IsActive", fixedAsset.IsActive,
                "@CapitalMoneyAccountCode", fixedAsset.CapitalMoneyAccountCode,
                "@ExpenseAccountCode", fixedAsset.ExpenseAccountCode,
                "@UnitID", fixedAsset.UnitId
            };
        }
    }
}
